package com.tbit.uqbike.client.service.impl;

import com.tbit.uqbike.center.pojo.MachineStatus;
import com.tbit.uqbike.client.constant.AccountConfigExtConstant;
import com.tbit.uqbike.client.constant.MapConstant;
import com.tbit.uqbike.client.constant.SysConstant;
import com.tbit.uqbike.client.dao.core.UserParkingGuidanceDao;
import com.tbit.uqbike.client.pojo.*;
import com.tbit.uqbike.client.pojo.newEntity.AccountConfigExt;
import com.tbit.uqbike.client.pojo.newEntity.ParkFeature;
import com.tbit.uqbike.client.service.*;
import com.tbit.uqbike.client.util.DateTimeUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: ShiGuang
 * @create: 2021-12-16 15:38
 * @description: 用户观看停车引导记录实现类
 */
@Service
public class UserParkingGuidanceServiceImpl implements UserParkingGuidanceService {

    private final static Logger log = LogManager.getLogger(UserParkingGuidanceServiceImpl.class);
    @Autowired
    private UserParkingGuidanceDao userParkingGuidanceDao;
    @Autowired
    private ParkPointService parkPointService;
    @Autowired
    private MachineBorrowService machineBorrowService;
    @Autowired
    private MachineService machineService;
    @Autowired
    private CalibrateService calibrateService;
    @Autowired
    private CacheService cacheService;
    @Autowired
    private ParkGuidanceConfigService parkGuidanceConfigService;
    @Autowired
    private MachineStatusService machineStatusService;

    /**
     * 通过userId和站点功能类型，判断用户是否观看过该类型的引导
     *
     * @param userId
     * @param featureType
     * @return
     */
    @Override
    public Integer getByUserIdAndFeatureType(Integer userId, Integer featureType) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("featureType", featureType);
        return userParkingGuidanceDao.getByUserIdAndFeatureType(params);
    }

    /**
     * 数据库新增用户观看引导记录
     */
    @Override
    public void addUserParkingGuidance(UserParkingGuidance userParkingGuidance) {
        userParkingGuidance.setLastTime(DateTimeUtil.getNowTime());
        userParkingGuidance.setCount(1);
        userParkingGuidanceDao.insert(userParkingGuidance);
    }

    /**
     * 判断是否需要向用户展示首次引导
     *
     * @param userId
     * @param lo
     * @param la
     * @param mapType
     */
    @Override
    public Map<String, Object> getParkingGuidance(Integer userId, Double lo, Double la, Integer mapType) {
        Map<String, Object> map = new HashMap<>();
        UserParkingGuidance userParkingGuidance = new UserParkingGuidance();
        ParkGuidanceConfig parkGuidanceConfig;
        userParkingGuidance.setCount(0);
        map.put("userParkingGuidance", userParkingGuidance);

        // 如果没有经纬度信息，则不观看首次引导
        if (lo == null || la == null) {
            return map;
        }
        // 判断是否有借车信息
        MachineBorrow machineBorrow = machineBorrowService.getByUserId(userId);
        if (machineBorrow == null) {
            return map;
        }
        // 获取区域id
        Integer machineId = machineBorrow.getMachineId();
        Machine machine = machineService.getByMachineId(machineId);
        Integer accountId = machine.getAccountId();

        /*
           获取还车站点id
           根据当前还车位置获取站点功能类型
           站点功能类型：0:站点道钉功能；1:RFID；3:脚撑；4:摄像头功能
         */
        Point point;
        point = new Point(lo, la);
        point = calibrateService.calibrate(point, mapType, SysConstant.ORIGINAL);
        Point checkPoint;
        Integer parkPointId;
        // 使用终端位置获取还车站点id
        MachineStatus machineStatus = machineStatusService.getByMachineNO(machine.getMachineNO(), MapConstant.MAP_GOOGLE);
        if (machineStatus != null && machineStatus.getLon() != null && machineStatus.getLat() != null) {
            checkPoint = new Point(machineStatus.getLon(), machineStatus.getLat());
            parkPointId = parkPointService.checkInParkPoint(accountId, checkPoint, machineId, null);
            // 使用手机位置获取还车站点id
        } else {
            parkPointId = parkPointService.checkInParkPoint(accountId, point, machineId, null);
        }
        if (parkPointId == null) {
            return map;
        }

        // 网页管理平台是否配置了首次还车引导视频或者图片
        parkGuidanceConfig = parkGuidanceConfigService.getByAccountId(accountId);
        if (parkGuidanceConfig == null) {
            return map;
        }
        // 站点是否开启了功能
        ParkFeature parkFeature = cacheService.getParkFeature(parkPointId, null);
        if (parkFeature == null) {
            return map;
        }

        // 用户是否观看过
        Integer featureType = parkFeature.getFeatureType();
        Integer count = getByUserIdAndFeatureType(userId, featureType);

        // 区域扩展配置是否开启了首次还车引导
        Integer button = 0;
        List<AccountConfigExt> accountConfigExts = cacheService.getAccountConfigExt(accountId);
        if (accountConfigExts.size() > 0) {
            for (AccountConfigExt a : accountConfigExts) {
                if (AccountConfigExtConstant.ACCOUNTCONFIG_USER_GUIDANCE.equalsIgnoreCase(a.getParamKey())) {
                    button = Integer.valueOf(a.getParamValue());
                }
            }
        }

        // 站点功能开启，区域扩展配置开启，用户没有看过
        if (parkFeature.getEnable() == 1 && button == 1 && count == null) {
            userParkingGuidance.setAccountId(accountId);
            userParkingGuidance.setFeatureType(featureType);
            map.put("userParkingGuidance", userParkingGuidance);
            map.put("parkGuidanceConfig", parkGuidanceConfig);
            return map;
        }
        return map;
    }
}

